package com.momo.service.sysSetting;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.momo.common.req.sysSetting.RouteReq;
import com.momo.common.req.sysSetting.route.RouteDefinition;
import com.momo.mapper.dao.RoutesDAO;
import com.momo.mapper.dataobject.RoutesDO;
import com.momo.rabbitmq.route.SendRouteToGateway_fanout;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by MOMO on 2019/2/27.
 * 动态路由
 */
@Service
@Slf4j
public class SysRoutesService {
    @Autowired
    private RoutesDAO routesDAO;
    @Autowired
    private SendRouteToGateway_fanout sendRouteToGateway_fanout;

    /**
     *  rabbitmq 新增或者更新 网关route
     *
     * @param routeReq
     */
    public void addRouteToGatewayLocalMapByMQ(RouteReq routeReq) {
        RoutesDO routesDO = routesDAO.getByPId(routeReq.getPId());
        RouteDefinition routeDefinition = RouteDefinition.DOtransformVO(routesDO);
        String routeStr = JSONObject.toJSONString(routeDefinition);
        sendRouteToGateway_fanout.senRoute(routeStr, null);
    }

    /**
     * 数据库路由分页查询
     *
     * @param routeReq
     * @return
     */
    public PageInfo<RouteDefinition> getRouteAll(RouteReq routeReq) {
        PageHelper.startPage(routeReq.getPageNum(), routeReq.getPageSize(), "p_id desc");
        List<RoutesDO> list = routesDAO.getRouteAll();
        PageInfo<RoutesDO> pageInfo = new PageInfo<>(list);
        List<RoutesDO> routesDOS = pageInfo.getList();
        List<RouteDefinition> routeReqs = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(routesDOS)) {
            routesDOS.forEach(routesDO -> {
                RouteDefinition routeDefinition = RouteDefinition.DOtransformVO(routesDO);
                routeReqs.add(routeDefinition);
            });
        }
        PageInfo<RouteDefinition> info = new PageInfo<>();
        info.setList(routeReqs);
        info.setPageSize(pageInfo.getPageSize());
        info.setPageNum(pageInfo.getPageNum());
        info.setTotal(pageInfo.getTotal());
        return info;
    }

    @Transactional
    public void addRoute(RouteDefinition routeDefinition) {
        RoutesDO routesDO = new RoutesDO();
        routesDO.setSysId(routeDefinition.getId());
        routesDO.setSysUri(routeDefinition.getUri().toString());
        routesDO.setSysFilters(JSONObject.toJSONString(routeDefinition.getFilters()));
        routesDO.setSysOrder(routeDefinition.getOrder());
        routesDO.setSysPredicates(JSONObject.toJSONString(routeDefinition.getPredicates()));
        routesDO.setSysName(routeDefinition.getSysName());
        routesDAO.insertSelective(routesDO);
    }

    @Transactional
    public void modifyRoute(RouteDefinition routeDefinition) {
        RoutesDO routesDO = new RoutesDO();
        routesDO.setSysId(routeDefinition.getId());
        routesDO.setSysUri(routeDefinition.getUri().toString());
        routesDO.setSysFilters(JSONObject.toJSONString(routeDefinition.getFilters()));
        routesDO.setSysOrder(routeDefinition.getOrder());
        routesDO.setSysPredicates(JSONObject.toJSONString(routeDefinition.getPredicates()));
        routesDO.setSysName(routeDefinition.getSysName());
        routesDAO.updateByPrimaryKeySelective(routesDO);
    }
}
